package com.Action;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.project.bean.Contract;

public class ContractReadExcel {
     public ContractReadExcel(){}
	public int ContractReadExcel(String path) {
		// TODO Auto-generated method stub
        int result =0;
		// 引入需要解析的EXCEL文件
		File file = new File(path);
		System.out.println("aaa");
		try {
			HSSFWorkbook workbook = new HSSFWorkbook(FileUtils.openInputStream(file));
			// 获取第一个工作表
			// HSSFSheet sheet = workbook.getSheet("sheet0");
			HSSFSheet sheet = workbook.getSheetAt(0);
			int firstRowNum = 0;
			// 获取sheet中最后一行行号
			int lastRowNum = sheet.getLastRowNum();
			System.out.println(lastRowNum);
			List<Contract> cl = new ArrayList<>();
			for (int i = 2; i <=lastRowNum; i++) {
				HSSFRow row = sheet.getRow(i);
				// 获取当前最后单元格列号
				int lastCellNum = row.getLastCellNum();
				Contract c = new Contract();
				// for(int j=0;j<lastCellNum;++j) {
				// HSSFCell cell =row.getCell(j);
				// String value = cell.getStringCellValue();
				// cell.setCellValue("aaaaa");
				 //System.out.print(value + " ");
				// }
				int j = 0;
				HSSFCell cell = row.getCell(j);
				System.out.println(cell);
				String contractid = cell.getStringCellValue();
				c.setContractid(contractid);
				
				cell = row.getCell(++j);
				String id = cell.getStringCellValue();
				c.setId(id);
				System.out.println(cell);
				
				cell = row.getCell(++j);
				String chinesename = cell.getStringCellValue();
				c.setChinesename(chinesename);
				cell = row.getCell(++j);
				String englishname = cell.getStringCellValue();
				c.setEnglishname(englishname);
				cell = row.getCell(++j);
				String sex = cell.getStringCellValue();
				c.setSex(sex);
				cell = row.getCell(++j);
				String birthday = cell.getStringCellValue();
				c.setBirthday(birthday);
				cell = row.getCell(++j);
				String coursename = cell.getStringCellValue();
				c.setCoursename(coursename);
				cell = row.getCell(++j);
				String totalclass = cell.getStringCellValue();
				c.setTotalclass(totalclass);
				cell = row.getCell(++j);
				String restclass = cell.getStringCellValue();
				c.setRestclass(restclass);
				cell = row.getCell(++j);
				String tuition = cell.getStringCellValue();
				c.setTuition(tuition);
				cell = row.getCell(++j);
				String chargestate = cell.getStringCellValue();
				c.setChargestate(chargestate);
				cell = row.getCell(++j);
				String signtype = cell.getStringCellValue();
				c.setSigntype(signtype);
				cell = row.getCell(++j);
				String counselor = cell.getStringCellValue();
				c.setCounselor(counselor);
				cell = row.getCell(++j);
				String phonecounselor = cell.getStringCellValue();
				c.setPhonecounselor(phonecounselor);
				cell = row.getCell(++j);
				String chineseteacher = cell.getStringCellValue();
				c.setChineseteacher(chineseteacher);
				cell = row.getCell(++j);
				String englishteacher = cell.getStringCellValue();
				c.setEnglishteacher(englishteacher);
				cell = row.getCell(++j);
				String username =cell.getStringCellValue();
				c.setUsername(username);
				cl.add(c);
			}
			System.out.println("aaa");
			ContractInputDB idb=new ContractInputDB();
			result =idb.InputDB(cl);;
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
             return result;
	}

}
